package demo1;

class OuterClass2 {


    public int data1 = 1;
    private int data2 = 2;
    public static int data3 = 3;

    //非静态内部类
    class InnerClass2 {
        public int data1 = 11;
        public int data4 = 4;
        private int data5 = 5;
        //在实例内部中，不能定义静态的成员变量
        //若要定义，则加final  ->>常量是在程序编译的时候 就能确定的
        public static final int data6 = 6;

        public void func() {
            System.out.println("InnerClass::func()");
            System.out.println(data1);//若有相同的变量名，则输出最近的变量值，就近原则
            //若要访问较远的变量，则需要：
            System.out.println(OuterClass2.this.data1);
            System.out.println(data2);
            System.out.println(data3);
            System.out.println(data4);
            System.out.println(data5);
            System.out.println(data6);
        }
    }

    public void test() {
        InnerClass2 innerClass2 = new InnerClass2();
        System.out.println(data1);
        System.out.println(data2);
        System.out.println(data3);
        System.out.println(innerClass2.data4);
        System.out.println(innerClass2.data5);
        System.out.println(InnerClass2.data6);
    }

}
public class Test2 {
    public static void main(String[] args) {
        OuterClass2 outerClass2 = new OuterClass2();
        OuterClass2.InnerClass2 innerClass2 = outerClass2.new InnerClass2();
        innerClass2.func();
    }
    //静态内部类更划算，静态内部类不需要拿外部类的对象
    //实例内部类比较麻烦，需要外部类的对象，而静态内部类不需要
}
